<template>
  <div class="app-container">
    <el-card>
      <el-form :model="queryParams" ref="queryRef" :inline="true" label-width="68px">
        <el-form-item label="设备" prop="equipmentId">
          <el-select v-model="queryParams.equipmentId" placeholder="请选择设备" clearable>
            <el-option
              v-for="dict in equipmentList"
              :key="dict.value"
              :label="dict.equipmentName "
              :value="dict.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="报修时间" style="width: 308px">
          <el-date-picker
            v-model="daterangeRepairTime"
            value-format="YYYY-MM-DD HH:mm:ss"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            :default-time="[new Date(2000, 1, 1, 0, 0, 0), new Date(2000, 1, 1, 23, 59, 59)]"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="报修状态" prop="repairStatus">
          <el-select v-model="queryParams.repairStatus" placeholder="请选择报修状态" clearable>
            <el-option
              v-for="dict in repair_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="报修人" prop="repairer">
          <el-input
            v-model="queryParams.repairer"
            placeholder="请输入报修人"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <el-card class="mt20">

      <el-row :gutter="10" class="mb8" type="flex" justify="space-between">
        <el-col :span="6"><span style="font-size: large">维修记录</span></el-col>
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
            v-hasPermi="['wms:repairRecords:add']"
          >新增
          </el-button>
          <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            v-hasPermi="['wms:repairRecords:export']"
          >导出
          </el-button>
        </el-col>
      </el-row>

      <el-table v-loading="loading" :data="repairRecordsList" border class="mt20">

        <el-table-column label="设备" prop="equipmentId">
          <template #default="scope">
            <span>{{ getEquipmentName(scope.row.equipmentId).equipmentName }}</span>
          </template>
        </el-table-column>

        <el-table-column label="报修时间" align="center" prop="repairTime" width="180">
          <template #default="scope">
            <span>{{ parseTime(scope.row.repairTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="报修状态" prop="repairStatus">
          <template #default="scope">
            <dict-tag :options="repair_status" :value="scope.row.repairStatus"/>
          </template>
        </el-table-column>
        <el-table-column label="报修人" prop="repairer"/>
        <el-table-column label="备注" prop="context"/>
        <el-table-column label="操作" align="right" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
                       v-hasPermi="['wms:repairRecords:edit']">修改
            </el-button>
            <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
                       v-hasPermi="['wms:repairRecords:remove']">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-row>
        <pagination
          v-show="total>0"
          :total="total"
          v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize"
          @pagination="getList"
        />
      </el-row>

    </el-card>
    <!-- 添加或修改维修记录对话框 -->
    <el-drawer :title="title" v-model="open" size="50%" append-to-body>
      <el-form ref="repairRecordsRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="设备" prop="equipmentId">
          <el-select v-model="form.equipmentId" placeholder="请选择设备" clearable>
            <el-option
              v-for="dict in equipmentList"
              :key="dict.value"
              :label="dict.equipmentName "
              :value="dict.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="报修时间" prop="repairTime">
          <el-date-picker clearable
                          v-model="form.repairTime"
                          type="datetime"
                          value-format="YYYY-MM-DD HH:mm:ss"
                          placeholder="请选择报修时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="报修状态" prop="repairStatus">
          <el-select v-model="form.repairStatus" placeholder="请选择报修状态">
            <el-option
              v-for="dict in repair_status"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="报修人" prop="repairer">
          <el-input v-model="form.repairer" placeholder="请输入报修人"/>
        </el-form-item>
        <el-form-item label="备注" prop="context">
          <el-input v-model="form.context" placeholder="请输入备注"/>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-drawer>
  </div>
</template>

<script setup name="RepairRecords">
import {
  listRepairRecords,
  getRepairRecords,
  delRepairRecords,
  addRepairRecords,
  updateRepairRecords
} from "@/api/wms/repairRecords";
import {listEquipment} from "@/api/wms/equipment";

const {proxy} = getCurrentInstance();
const {repair_status} = proxy.useDict('repair_status');

const repairRecordsList = ref([]);
const open = ref(false);
const buttonLoading = ref(false);
const loading = ref(true);
const ids = ref([]);
const total = ref(0);
const title = ref("");
const daterangeRepairTime = ref([]);
const equipmentList = ref([]);
const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    equipmentId: undefined,
    repairTime: undefined,
    repairStatus: undefined,
    repairer: undefined,
  },
  rules: {
    id: [
      {required: true, message: "不能为空", trigger: "blur"}
    ],
    equipmentId: [
      {required: true, message: "设备不能为空", trigger: "blur"}
    ],
    repairTime: [
      {required: true, message: "报修时间不能为空", trigger: "blur"}
    ],
    repairStatus: [
      {required: true, message: "报修状态不能为空", trigger: "change"}
    ],
    repairer: [
      {required: true, message: "报修人不能为空", trigger: "blur"}
    ],
    context: [
      {required: true, message: "备注不能为空", trigger: "blur"}
    ],
  }
});

const {queryParams, form, rules} = toRefs(data);

// 获取巡检记录下拉框数据
listEquipment().then(response => {
  equipmentList.value = response.rows;
  console.log(equipmentList.value);
});

//通过设备id转化为名字
function getEquipmentName(equipmentId) {
  const equipment = equipmentList.value.find(emp => emp.id === equipmentId);
  return equipment;
}

/** 查询维修记录列表 */
function getList() {
  loading.value = true;
  queryParams.value.params = {};
  if (null != daterangeRepairTime && '' != daterangeRepairTime) {
    queryParams.value.params["beginRepairTime"] = daterangeRepairTime.value[0];
    queryParams.value.params["endRepairTime"] = daterangeRepairTime.value[1];
  }
  listRepairRecords(queryParams.value).then(response => {
    repairRecordsList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    equipmentId: null,
    repairTime: null,
    repairStatus: null,
    repairer: null,
    context: null,
    createTime: null,
    updateTime: null,
    createBy: null,
    updateBy: null
  };
  proxy.resetForm("repairRecordsRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  daterangeRepairTime.value = [];
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加维修记录";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getRepairRecords(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改维修记录";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["repairRecordsRef"].validate(valid => {
    if (valid) {
      buttonLoading.value = true;
      if (form.value.id != null) {
        updateRepairRecords(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        }).finally(() => {
          buttonLoading.value = false;
        });
      } else {
        addRepairRecords(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        }).finally(() => {
          buttonLoading.value = false;
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除维修记录编号为"' + _ids + '"的数据项？').then(function () {
    loading.value = true;
    return delRepairRecords(_ids);
  }).then(() => {
    loading.value = true;
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
  }).finally(() => {
    loading.value = false;
  });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('wms/repairRecords/export', {
    ...queryParams.value
  }, `repairRecords_${new Date().getTime()}.xlsx`)
}

getList();
</script>
